﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.Theme.Default;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.ConsoleApp
{
    public partial class MainWindow
    {
        [Subscribe]
        public void OnReceiveUISwitchSolutionEvent(UIShowSwitchSolutionViewEvent @event)
        {
            // 显示选择方案的界面
            if(CPPEI.Coolzon.Theme.ThemeManager.GetCurrentThemeInst() is Coolvis.Console.Theme.IThemeV2)
            {
                CPPEI.Coolzon.UI.View selectSolutionView = (CPPEI.Coolzon.Theme.ThemeManager.GetCurrentThemeInst() as Coolvis.Console.Theme.IThemeV2).CreateSelectSolutionView(UIContext.Current.SolutionName, true, true);
                selectSolutionView.Close += selectSolutionView_Close;
                _ctlViewContainer.SwitchSubView(selectSolutionView);
                selectSolutionView.Active();
            }
            else
            {
                CPPEI.Coolzon.UI.View selectSolutionView = new UI.Views.SelectSolutionView(UIContext.Current.SolutionName, true);
                selectSolutionView.Close += selectSolutionView_Close;
                _ctlViewContainer.SwitchSubView(selectSolutionView);
                selectSolutionView.Active();
            }
        }

        [Subscribe]
        public void OnReceiveUIHideSwitchSolutionViewEvent(UIHideSwitchSolutionViewEvent @event)
        {
            // 关闭选择方案的界面
            _ctlViewContainer.Back();
        }

        [Subscribe]
        public void OnReceiveResponseSwitchSolutionEvent(UISwitchSolutionEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                lock (_lockLoadingView)
                {
                    if (!UIContext.Current.IsLoadingSolution)
                    {
                        if(UIContext.Current.RenderView != null)
                        {
                            // 让当前场景destory
                            (UIContext.Current.RenderView as RenderView).ClearAllParts();
                        }

                        UIContext.Current.IsEnterDomainByCreate = true;
                        ReconnectThread.Stop();


                        if (CPPEI.Coolzon.Theme.ThemeManager.GetCurrentThemeInst() is Coolvis.Console.Theme.IThemeV2)
                        {
                            CPPEI.Coolzon.UI.View loadingView = (CPPEI.Coolzon.Theme.ThemeManager.GetCurrentThemeInst() as Coolvis.Console.Theme.IThemeV2).CreateLoadingView(@event.SolutionName,
                                CZEnvironment.GetSolutionFilePath(@event.SolutionName),
                                CZEnvironment.GetSolutionDirectoryPath(@event.SolutionName));
                            loadingView.Close += OnLoadingViewClose;
                            (loadingView as CPPEI.Coolzon.UI.Views.ILoadingView).LoadSolutionErrorEvent += LoadingView_LoadSolutionErrorEvent;
                            _ctlViewContainer.SwitchView(loadingView);
                            loadingView.Active();
                        }
                        else
                        {
                            CPPEI.Coolzon.UI.Views.LoadingView loadingView = new UI.Views.LoadingView(@event.SolutionName,
                                CZEnvironment.GetSolutionFilePath(@event.SolutionName),
                                CZEnvironment.GetSolutionDirectoryPath(@event.SolutionName));
                            loadingView.Close += OnLoadingViewClose;
                            loadingView.LoadSolutionErrorEvent += LoadingView_LoadSolutionErrorEvent;
                            _ctlViewContainer.SwitchView(loadingView);
                            loadingView.Active();
                        }
                    }
                }
            });
        }
    }
}
